using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
using System.IO;
using System.Collections;

namespace Cache
{
    

    public partial class frmSendToGPS : Form
    {
        //public string[] arrCaches;
        public ArrayList arrCaches = new ArrayList();
        
        public TextWriter TXT = new StreamWriter(Application.StartupPath + @"\temp\gps.txt");
        public frmSendToGPS()
        {
            InitializeComponent();
        }

        private void frmSendToGPS_Load(object sender, EventArgs e)
        {
            //MessageBox.Show(arrCachescLength.ToString());
            cboGPSSerialPort.Items.Add("COM1");
            cboGPSSerialPort.Items.Add("COM2");
            cboGPSSerialPort.Items.Add("COM3");
            cboGPSSerialPort.Items.Add("COM4");
            cboGPSSerialPort.Items.Add("COM5");
            cboGPSSerialPort.Items.Add("COM6");
            Options opt = new Options();

            cboGPSSerialPort.SelectedText = opt.GetOption("GPSComPort");

            if (opt.GetOption("GPSConnType").ToString() == "Serial")
                rdoGPSSerial.Checked = true;
            else
                rdoGPSUSB.Checked = true;

            cboGPSBrand.Items.Add("Garmin");
            cboGPSBrand.Items.Add("Magellan");
            cboGPSBrand.SelectedText = opt.GetOption("GPSBrand");


        }

        
        private void WriteTXTFile(string CacheName, string Desc, string Lat, string Lon, string Symbol)
        {

            //FORMAT LAT AND LON properly.
            if (Convert.ToDouble(Lat) > 0)
                Lat = "N" + Lat;
            else
                Lat = Lat.Replace("-", "S");

            if (Convert.ToDouble(Lon) < 0)
                Lon = Lon.Replace("-", "W");
            else
                Lon = "E" + Lon;

            string txtInput = null;
            
            txtInput = "Waypoint" + (char)9 + CacheName + (char)9 + Desc + (char)9 + "User Waypoint";
            txtInput += (char)9 + Lat + " " + Lon + (char)9 + " " + (char)9 + " " + (char)9 + " " + (char)9 + " ";
            txtInput += "Symbol & Name" + (char)9 + "Unknown" + (char)9 + Symbol;

            TXT.WriteLine(txtInput);
            //MessageBox.Show("File Written");               

        }

        private void cmdSendToGPS_Click(object sender, EventArgs e)
        {
            TXT.WriteLine("Grid" + (char)9 + "Lat/Lon hddd.ddddd" + (char)176);
            TXT.WriteLine("Datum" + (char)9 + "WGS 84");
            TXT.WriteLine("");
            TXT.WriteLine("Header" + (char)9 + "Name" + (char)9 + "Description" + (char)9 + "Type" + (char)9 + "Position" + (char)9 + "Altitude" + (char)9 + "Depth" + (char)9 + "Proximity" + (char)9 + "Temperature" + (char)9 + "DisplayMode" + (char)9 + "Color" + (char)9 + "Symbol" + (char)9 + "Facility" + (char)9 + "City" + (char)9 + "State" + (char)9 + "Country" + (char)9 + "DateModified" + (char)9 + "Link" + (char)9 + "Categories");
            TXT.WriteLine("");

            
            IEnumerator myEnum = arrCaches.GetEnumerator();
            //MessageBox.Show(arrCaches.Count.ToString());
            pbSendStatus.Maximum = arrCaches.Count;
            txtStatus.Text = "Writing Caches to temorary file...\r\n";

            while (myEnum.MoveNext())
            {
                try
                {
                    //MessageBox.Show(myEnum.Current.ToString());
                    SQLiteConnection conn = new SQLiteConnection("data source=" + Application.StartupPath + @"\GeoCaches.db");

                    SQLiteDataReader rdr = null;
                    SQLiteCommand cmd = new SQLiteCommand("SELECT Name, Desc, Latitude, Longitude, Sym FROM tblGeoCache WHERE Name = '" + myEnum.Current.ToString() + "'", conn);

                    conn.Open();

                    rdr = cmd.ExecuteReader();

                    while (rdr.Read())
                    {
                        WriteTXTFile(rdr["Name"].ToString(), rdr["Desc"].ToString(), rdr["Latitude"].ToString(), rdr["Longitude"].ToString(), rdr["Sym"].ToString());
                        //MessageBox.Show(rdr["Name"].ToString() + "\r\n" + rdr["Latitude"].ToString() + "\r\n" + rdr["Longitude"].ToString() + "\r\n" + rdr["Sym"].ToString());
                    }

                }//END TRY
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            } //END WHILE
            TXT.Close();
            txtStatus.Text += "Done writing temp file!";
        }

        private void rdoGPSSerial_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoGPSSerial.Checked == true)
                cboGPSSerialPort.Enabled = true;
            else
                cboGPSSerialPort.Enabled = false;
        }

        private void rdoGPSUSB_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoGPSSerial.Checked == true)
                cboGPSSerialPort.Enabled = true;
            else
                cboGPSSerialPort.Enabled = false;
        }
    }
}